<template>
    <div class="vueManage">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 流程定义
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>


        <div class="container">
            <div class="handle-box">
                <!--<el-button-->
                <!--type="primary"-->
                <!--icon="el-icon-search"-->
                <!--@click="addVisible = true"-->
                <!--class="tianjia"-->
                <!--&gt;添加-->
                <!--</el-button>-->
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable">
                <el-table-column type="index" width="55" align="center" label="序号" class-name="table"></el-table-column>
                <el-table-column prop="name" label="流程方案" width="120" class-name="table"></el-table-column>
                <el-table-column prop="category" label="流程类别" width="100" class-name="table"></el-table-column>
                <el-table-column prop="deployTime" label="部署时间" width="100" :formatter="dateFormatter"
                                 class-name="table"></el-table-column>

                <el-table-column label="操作" width="180" align="center" class-name="table">
                    <template slot-scope="scope">

                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleProcess(scope.$index, scope.row)"
                        >填写流程
                        </el-button>


                        <!--<el-button-->
                        <!--type="text"-->
                        <!--icon="el-icon-edit"-->
                        <!--@click="handleEdit(scope.$index, scope.row)"-->
                        <!--&gt;编辑-->
                        <!--</el-button>-->
                        <!--<el-button-->
                        <!--type="text"-->
                        <!--icon="el-icon-delete"-->
                        <!--class="red"-->
                        <!--@click="handleDelete(scope.$index, scope.row)"-->
                        <!--&gt;删除-->
                        <!--</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="cur_page"
                        :page-size="pagesize"
                        :total="totalRecords"
                        :page-sizes="[5, 10, 20, 40]"
                        layout="total, sizes, prev, pager, next, jumper"
                ></el-pagination>
            </div>
        </div>


        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="30%">

            <el-form id="addForm" ref="addForm" :model="addForm" :rules="FormRules" label-width="100px">
                <el-form-item label="用户名" prop="traceCode">
                    <el-input v-model="addForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="traceContent">
                    <el-input v-model="addForm.userSex"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="traceReason">
                    <el-input v-model="addForm.userPhone"></el-input>
                </el-form-item>
                <el-form-item label="微信号" prop="traceResult">
                    <el-input v-model="addForm.userWechat"></el-input>
                </el-form-item>
                <el-form-item label="水类型" prop="servicesWtid">

                    <!--  v-model的值为当前被选中的el-option的 value 属性值-->
                    <el-select v-model="addForm.servicesWtid" placeholder="水类型">.
                        <!-- wt 是在date代码块儿里面预先定义的变量
                        :label 是显示的名字,这里是固定写.value 就行
                        :key
                        :value 是传递到后台的内容
                         -->
                        <el-option
                                v-for="item in wt"
                                :key="item.servicesWtid"
                                :label="item.value"
                                :value="item.servicesWtid"></el-option>
                    </el-select>

                </el-form-item>


            </el-form>


            <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
        </el-dialog>


        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
        </el-dialog>


        <!-- 流程弹出框 -->
        <el-dialog title="填写流程" :visible.sync="processVisible" width="30%">

            <el-form ref="processPoint" :model="processPoint" label-width="100px">
                <!--流程id-->
                <el-input v-model="processPoint.flowId" type="hidden"></el-input>
                <!--登录人id-->
                <el-input v-model="processPoint.loginId" type="hidden"></el-input>
                <el-form-item label="流程方案" prop="name">
                    <el-input v-model="processPoint.name" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="流程类别" prop="category">
                    <el-input v-model="processPoint.category" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="原因" prop="cause">
                    <el-input v-model="processPoint.cause"></el-input>
                </el-form-item>


            </el-form>

            <span slot="footer" class="dialog-footer">
        <el-button @click="processVisible = false">取 消</el-button>
        <el-button type="primary" @click="processInsert">确 定</el-button>
      </span>
        </el-dialog>


        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <!--        :rules="FormRules"  是开启校验校验是和 prop 对应的-->
            <el-form ref="formPoint" :model="formPoint" label-width="100px" :rules="FormRules">
                <el-form-item label="姓名" prop="traceContent">
                    <el-input v-model="formPoint.userName"></el-input>
                </el-form-item>
                <el-form-item label="用户手机号" prop="userPhone">
                    <el-input v-model="formPoint.userPhone"></el-input>
                </el-form-item>
                <el-form-item label="是否删除" prop="traceResult">
                    <el-radio v-model="formPoint.userIsdel" label="1">是</el-radio>
                    <el-radio v-model="formPoint.userIsdel" label="0">否</el-radio>
                </el-form-item>
                <el-form-item label="创建时间" prop="traceNewadd">
                    <el-input v-model="formPoint.userCreatetime"></el-input>
                </el-form-item>
            </el-form>


            <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
        </el-dialog>


    </div>


</template>

<script>

    import {config, judgeResponseCode} from '../../utils/config'


    export default {
        name: "vueManage",
        data() {
            return {
                tableData: [],
                wt: [],
                //对编辑模态框进行校验
                FormRules: {
                    userPhone: [{message: "用户手机号不能为空", required: true, trigger: "blur"}],
                },

                cur_page: 1,
                pagesize: 5,
                totalRecords: 0,
                totalPages: 0,
                processVisible: false,
                delVisible: false,
                addVisible: false,
                editVisible: false,
                //添加模态框
                addForm: {},
                formPoint: {
                    // userIsdel:0
                }, processPoint: {
                    // userIsdel:0
                },

            };

        },
        created() {
            this.selectByPage();
            // this.getwt()
        },
        computed: {},
        methods: {

            processInsert() {
                var res = this;
                var setData = this.processPoint;
                this.$axios({
                    url: "/api/askForLeave/insert",
                    method: "post",
                    data: setData
                })
                    .then(function (response) {
                        res.processVisible = false;
                        res.selectByPage();

                    })
                    .catch(function (error) {
                        res.$message.error("查询失败: " + error);
                    });
            },


            dateFormatter(row, column) {

                //updateTime 是你后端返回给前端的字段,
                //row代表每一行
                let datetime = row.deployTime;
                if (datetime) {
                    datetime = new Date(datetime);
                    let y = datetime.getFullYear() + "年";
                    let mon = datetime.getMonth() + 1 + "月";
                    let d = datetime.getDate() + "日";
                    return y + mon + d;
                }
            },
            stateFormat(row, column) {
                // console.log(column);  //获取的是你使用stateFormat 函数的那行的属性
                // console.log(row); // row 可以取出当前行的所有值
                // console.log(row.userId)  //获取出来当前行的用户id
                // console.log(column.property);  //userIsdel
                if (row.userIsdel === 0) {
                    return '未删除'
                } else {
                    return '已删除'
                }
            },


            // 保存编辑
            saveEdit() {
                var res = this;
                //获取表单的数据
                var setData = this.formPoint;
                //先获取校结果,
                this.$refs.formPoint.validate(valid => {
                    // 如果校验失败就走这里
                    if (!valid) {
                        res.$message.success("格式不正确");
                    } else {
                        //如果校验通过就走这里,也就是可以访问axios
                        this.$axios({
                            url: "/api/waterUser",
                            method: "put",

                            data: setData,
                            headers: {
                                contentType: "application/json; charset=utf-8",
                                headers: {Authorization: localStorage.getItem("jwtToken")}
                            }
                        })
                            .then(function (response) {

                                if (response.status == 200) {
                                    res.editVisible = false;
                                    res.$message.success("修改成功");

                                    res.selectByPage();

                                    if (res.tableData[res.idx].id === res.id) {
                                        res.$set(res.tableData, res.idx, res.formPoint);
                                    } else {
                                        for (let i = 0; i < res.tableData.length; i++) {
                                            if (res.tableData[i].id === res.id) {
                                                res.$set(res.tableData, i, res.formPoint);
                                                return;
                                            }
                                        }
                                    }
                                }


                            })
                            .catch(function (error) {
                                // res.$message.success("修改失败！");
                                console.log(error);
                            });
                    }
                })
            }
            ,
            selectByPage() {
                var res = this;
                this.$axios({

                    url: "/api/processDefinition/selectAll",
                    method: "get",

                })
                    .then(function (response) {

                        judgeResponseCode(response.data);

                        if (response.data.code === 200) {

                            // alert(JSON.stringify(response.data.data))
                            res.tableData = response.data.data;

                        }

                        res.totalRecords = response.data.data.total; //总条数
                        // res.totalRecords = response.data.length; //总条数

                    })
                    .catch(function (error) {
                    });

            },
            // 添加
            saveAdd() {
                var res = this;
                var setData = this.addForm;

                this.$axios({
                    url: "/api/waterUser/add",
                    method: "post",
                    data: setData
                })
                    .then(function (response) {
                        // if (response.data.code == 200) {
                        //     // res.tableData = response.data.data.list;
                        // }
                        if (response.status == 200) {
                            res.$message.success("添加成功");
                            // alert("添加用户成功")
                            //关闭模态框
                            res.addVisible = false;
                            // 添加成功清理掉模态框的预留的内容,如果不做这部操作的话,
                            //你添加成功之后模态框还会有原来的内容
                            document.getElementById("addForm").reset();
                            res.selectByPage();

                        } else {
                            alert("添加用户失败")
                        }


                        // res.totalRecords = response.data.length; //总条数
                        // console.log(response.data);

                    })
                    .catch(function (error) {
                        // res.$message.error("查询失败: " + error);
                        // console.log(error);
                    });

            },


            handleProcess(index, row) {
                this.idx = index;
                this.id = row.id;
                this.processPoint = {
                    loginId: localStorage.getItem("loginId"),
                    flowId: row.id,
                    name: row.name,
                    category: row.category

                }
                ;
                this.processVisible = true;
            },


            handleEdit(index, row) {
                this.idx = index;
                this.id = row.userId;
                this.formPoint = {
                    userId: row.userId,
                    userName: row.userName,
                    userSex: row.userSex,
                    userPhone: row.userPhone,
                    userIsdel: row.userIsdel,
                    userCreatetime: row.userCreatetime,
                };
                this.editVisible = true;
            },
            // 确定删除
            deleteRow() {
                var res = this;
                // console.log(this.id);
                // alert(res.id)
                this.$axios({
                    url: "/api/waterUser?id=" + res.id,
                    method: "delete",
                    // data: {id: res.id}
                })
                    .then(function (response) {
                        if (response.status <= 200) {
                            res.$message.success("删除成功");
                            res.delVisible = false;

                            res.selectByPage();

                            // if (res.tableData[res.idx].id === res.id) {
                            //     res.tableData.splice(res.idx, 1);
                            // } else {
                            //     for (let i = 0; i < res.tableData.length; i++) {
                            //         if (res.tableData[i].id === res.id) {
                            //             res.tableData.splice(i, 1);
                            //             return;
                            //         }
                            //     }
                            // }
                        }
                    })
                    .catch(function (error) {
                        res.$message.success("查询失败！");
                        console.log(error);
                    });
            }
            ,

            handleDelete(index, row) {
                this.idx = index;
                // 获取 userid
                this.id = row.userId;
                this.delVisible = true;
            }
            ,
            //每页显示数改变
            handleSizeChange(val) {
                this.pagesize = val;
                this.selectByPage();
            }
            ,
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.selectByPage();
            }
        }
    }
</script>

<style scoped>
    .huizong {
        height: 30px;
        width: 400px;
        position: relative;
    }

    .huizong span {
        height: 30px;
        width: 400px;
        position: absolute;
        bottom: -60px;
        z-index: 999;
    }

    .tianjia {
        margin-left: 10px;
    }

    .sousuo {
        margin-right: 10px;
    }

    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .outup {
        font-size: 12px;
        color: #fff;
        background-color: #409eff;
        display: inline-block;
        border-radius: 3px;
        padding: 8px 15px;
        margin-left: 10px;
        margin-top: 3px;
    }

    .upload-demo {
        height: 30px;
        width: 100px;
        display: inline-block;
        margin-left: 10px;
    }

    .inup {
        margin-top: 10px;
    }
</style>
<style>
    .el-upload--text {
        width: 60px;
        height: 30px;
        display: inline-block;
        border: 0;
        overflow: visible;
    }

    .el-table--border {
        border: 1px solid black;
    }

    .el-table--border,
    .el-table--border th,
    .el-table td,
    .el-table th {
        border-right: 1px solid black;
        border-bottom-color: black;
        border-left-color: black;
    }

    .table {
        border-bottom-color: black !important;
        border-left-color: black !important;
    }
</style>
